package com.jnu.myAccount.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jnu.myAccount.MyTextInputWatcher;
import com.jnu.myAccount.R;
import com.jnu.myAccount.databinding.ActivityEditBinding;
import com.jnu.myAccount.databinding.FragmentInAndOutBinding;
import com.jnu.myAccount.databinding.ItemKindLayoutBinding;
import com.jnu.myAccount.databinding.LayoutItemBinding;
import com.jnu.myAccount.fragment.InAndOutFragment;
import com.jnu.myAccount.fragment.TransferFragment;
import com.jnu.myAccount.record.DataPackage;
import com.jnu.myAccount.viewModel.EditViewModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EditActivity extends AppCompatActivity {
    private InAndOutFragment inAndOutFragment = null;
    private EditViewModel editViewModel;
    private Intent intent;
    private List<String> tabTitles;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityEditBinding binding = ActivityEditBinding.inflate(getLayoutInflater());
        editViewModel=new ViewModelProvider(this).get(EditViewModel.class);
        intent = getIntent();
        setContentView(binding.getRoot());

        //初始化账目添加页面
        //****************************************************************************
        tabTitles = new ArrayList<>();
        tabTitles.add("收支");
        tabTitles.add("转账");

        TabLayout tabLayout = binding.layoutEditTab;
        ViewPager2 viewPager = binding.layoutEditContent;
        FragmentAdapter fragmentAdapter = new FragmentAdapter(this);
        viewPager.setAdapter(fragmentAdapter);

        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> tab.setText(tabTitles.get(position)));
        tabLayoutMediator.attach();
        //****************************************************************************

        //监听返回按钮点击事件
        binding.buttonReturn.setOnClickListener(v -> {
            setResult(RESULT_CANCELED, intent);
            finish();
        });

        //监听再记一笔按钮点击事件 => 保存账目后还原记账的Fragment
        binding.buttonAnotherRecord.setOnClickListener(v -> {
            editViewModel.completeFundFlowInfo();

            //还原金额输入框为0.00
            FragmentInAndOutBinding fragmentInAndOutBinding = inAndOutFragment.getFragmentInAndOutBinding();
            fragmentInAndOutBinding.inputArea.setText(R.string.value_hint);

            //获取选择的消费类型，据此找到第一次记账选择的图标位置
            String nowType = editViewModel.getType().getValue();
            List<String> typeList = DataPackage.getStaticKindList();
            int typeIndex = typeList.indexOf(nowType);

            // 从记账Fragment中得到RecyclerView
            // 从RecyclerView中得到GridView
            // 从GridView中得到ImageView和TextView
            RecyclerView recycleViewItem = fragmentInAndOutBinding.recycleViewItem;
            RecyclerView.ViewHolder selectedViewHolder = recycleViewItem.findViewHolderForLayoutPosition(typeIndex);
            View itemView = selectedViewHolder.itemView;
            ImageView imageView = itemView.findViewById(R.id.icon_kind_image);
            TextView textView = itemView.findViewById(R.id.kind_text);

            //还原图标及其名称的颜色
            imageView.clearColorFilter();
            textView.setTextColor(Color.rgb(0,0,0));

            //获取今天的年月日信息
            Date date = new Date(System.currentTimeMillis());
            String[] strings = MyTextInputWatcher.getStandardDateString(date).split("-");
            int year = Integer.parseInt(strings[0]);
            int month = Integer.parseInt(strings[1]);
            int dayOfMonth = Integer.parseInt(strings[2]);
            //更新日期、消费类型、收支类型、账户
            editViewModel.setDate(year,month,dayOfMonth);
            editViewModel.setType(0);
            editViewModel.setInOrOut(true);
            editViewModel.setAccount(editViewModel.getItems().getAccountList().get(0));

            Toast.makeText(inAndOutFragment.getContext(), "保存成功",Toast.LENGTH_LONG).show();
        });
    }

    //FragmentAdapter实现三个重要的函数
    //构造函数；重载createFragment【获得Tab对应的Fragment】；getItemCount【返回Tab数目】
    private class FragmentAdapter extends FragmentStateAdapter {
        public FragmentAdapter(FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position){
                case 1:{
                    return new TransferFragment();
                }
                default:{
                    inAndOutFragment = new InAndOutFragment();
                    return inAndOutFragment;
                }
            }
        }

        @Override
        public int getItemCount() {
            return 2;
        }
    }
}